package com.hanhanxiaochong.zhongxiangchuanmei.view.my.act;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hanhanxiaochong.zhongxiangchuanmei.R;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.AddressItemBean;
import com.hanhanxiaochong.zhongxiangchuanmei.presenter.AddressPresenter;
import com.hanhanxiaochong.zhongxiangchuanmei.util.SharedPreferencesUtil;
import com.hanhanxiaochong.zhongxiangchuanmei.util.ToastUtils;
import com.hanhanxiaochong.zhongxiangchuanmei.view.base.BaseActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.iview.IAddressView;
import com.hanhanxiaochong.zhongxiangchuanmei.view.my.adapter.AddressAdapter;
import com.hanhanxiaochong.zhongxiangchuanmei.widget.ConfirmDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Author : 贾桐
 * E-mail : 653773284@qq.com
 * Time   : 2018/5/9.
 * Desc   :
 */

public class AddressActivity extends BaseActivity<AddressPresenter> implements IAddressView {
    @BindView(R.id.img_left)
    ImageView imgLeft;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.recycler_address)
    RecyclerView recyclerAddress;
    @BindView(R.id.layout_addressyes)
    LinearLayout layoutAddressyes;
    @BindView(R.id.txt_down_create)
    TextView txtDownCreate;
    @BindView(R.id.layout_addressno)
    LinearLayout layoutAddressno;

    private AddressAdapter addressAdapter;
    private String token;
    private static final int REQUST_ADDRESS = 0x0004;
    //从哪里跳转,"my"我的,"pay"支付页面
    private String openFrom;
    private static final int RESULT_ADDRESS = 0x0001;

    public static Intent createIntent(Context context, String openFrom) {
        Intent intent = new Intent(context, AddressActivity.class);
        intent.putExtra("openFrom", openFrom);
        return intent;
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_addressmanagement);
        token = SharedPreferencesUtil.getsInstances(this).getLoginInfo().getToken();
        openFrom = getIntent().getStringExtra("openFrom");
    }

    @Override
    protected void initPresenter() {
        mPresenter = new AddressPresenter(this);
    }

    @Override
    protected void init() {
        setTopView();

        mPresenter.getAddressList(token);

        layoutAddressyes.setVisibility(View.GONE);
        layoutAddressno.setVisibility(View.VISIBLE);

        List<AddressItemBean> list = new ArrayList<>();
        recyclerAddress.setLayoutManager(new LinearLayoutManager(mContext));
        addressAdapter = new AddressAdapter(mContext, R.layout.item_address, list);
        recyclerAddress.setAdapter(addressAdapter);
        addressAdapter.setOnDeleteClickListenr(new AddressAdapter.OnClickListener() {
            @Override
            public void onDefaultClick(String address_id, String isDefault) {
                mPresenter.setDefaultAddress(token, address_id, isDefault);
            }

            @Override
            public void onDeleteClick(String address_id) {
                Bundle bundle = new Bundle();
                bundle.putString("content", "要删除该收货地址么");
                ConfirmDialog dialog = ConfirmDialog.newInstance(bundle);
                dialog.setOnConfirmListener(() -> {
                    mPresenter.deleteAddress(token, address_id);
                });
                dialog.show(getSupportFragmentManager(), "ConfirmDialog");
            }
        });
        addressAdapter.setmOnItemClickListener((adapter, position) -> {
            if (openFrom.equals("pay")) {
                AddressItemBean bean = addressAdapter.getDatas().get(position);
                Intent intent = new Intent();
                intent.putExtra("addressBean", bean);
                setResult(RESULT_ADDRESS, intent);
                finish();
            } else {
                mContext.startActivity(NewAddressActivity.createIntent(mContext, "edit", addressAdapter.getDatas().get(position)));
            }
        });
    }

    private void setTopView() {
        imgLeft.setImageResource(R.mipmap.icon_left);
        txtTitle.setText("管理收货地址");
        txtRight.setText("创建");
    }

    @OnClick({R.id.ll_leftview, R.id.ll_rightview, R.id.txt_down_create})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_leftview:
                finish();
                break;
            case R.id.ll_rightview:
                startActivityForResult(NewAddressActivity.createIntent(this, "new", null), REQUST_ADDRESS);
                break;
            case R.id.txt_down_create:
                startActivityForResult(NewAddressActivity.createIntent(this, "new", null), REQUST_ADDRESS);
                break;
        }
    }

    @Override
    public void setAddressList(List<AddressItemBean> data) {
        if (data.size() > 0) {
            layoutAddressno.setVisibility(View.GONE);
            layoutAddressyes.setVisibility(View.VISIBLE);
            addressAdapter.setDatas(data);
        } else {
            layoutAddressno.setVisibility(View.VISIBLE);
            layoutAddressyes.setVisibility(View.GONE);
        }
    }

    @Override
    public void deleteSuccess() {
        ToastUtils.showToast("删除成功");
        mPresenter.getAddressList(token);
    }

    @Override
    public void defaultChangeSuccess() {
        mPresenter.getAddressList(token);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null && data.getAction().equals("address")) {
            mPresenter.getAddressList(token);
        }
    }
}
